package dao;

import entry.Message;
import entry.User;
import org.springframework.beans.factory.annotation.Autowired;
import service.WebSocket;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MessageDao {


    // 根据 双方的id获取对话信息
    public List<Message> getMessagesById(int one, int two){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            List<Message> messages = new ArrayList<Message>();
            connection = JDBCUtil.getConnection();
            String sql = "select * from message where `from`=? and `to`=? or `from`=? and `to`=? order by sendTime";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, one);
            statement.setInt(2, two);
            statement.setInt(3, two);
            statement.setInt(4, one);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Message message = new Message();
                message.setFrom(resultSet.getInt("from"));
                message.setTo(resultSet.getInt("to"));
                message.setContent(resultSet.getString("content"));
                message.setSendTime(resultSet.getString("sendTime"));
                messages.add(message);
            }
            return messages;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }
    // 插入数据
    public void insertMessage(Message message){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into message values(?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, message.getFrom());
            statement.setInt(2, message.getTo());
            statement.setString(3, message.getContent());
            statement.setString(4, message.getSendTime());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    // 根据当前用户的 id查找其会话的对象
    public List<User> findTalker(int userId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            List<User> talkers = new ArrayList<User>();
            connection = JDBCUtil.getConnection();
            String sql = "select distinct * from user where userId=any((select distinct `from` from message where `to`=?) union (select distinct `to` from message where `from`=?));";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, userId);
            statement.setInt(2, userId);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                User talker = new User();
                talker.setUserId(resultSet.getInt("userId"));
                talker.setUsername(resultSet.getString("username"));
                talker.setPhoto(resultSet.getString("photo"));
                talkers.add(talker);
            }
            return talkers;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }
        return null;
    }

}
